<template>
	<view class="myQrCode">
		<template>
			<view class="topArea">
				<view class="noQrImageWrapper">
					<template v-if="initData">
						<image v-if="!imgUrl" class="qrImage updateImage" :src="baseUrl+initData" mode="aspectFill"></image>
						<image v-if="imgUrl" class="qrImage updateImage" :src="baseUrl+imgUrl" mode="aspectFill"></image>
						<tui-upload class="upload-wrap" :bucketType="bucketType" :clearFlag="clearFlag" :limit="limit" :useType="'qrCode'" @complete="getImgResult($event)">
							<view class="update-text">更改收款二维码</view>
						</tui-upload>
					</template>
					<template v-else>
						<view class="uploadText">请上传您的收款码</view>
						<tui-upload v-if="!imgUrl" class="upload-wrap" :bucketType="bucketType" :clearFlag="clearFlag" :limit="limit" :useType="'qrCode'" @complete="getImgResult($event)">
							<image class="noQrImage" src="~@/packageC/static/img/upload.png"></image>
						</tui-upload>
						<image v-if="imgUrl" class="qrImage" :src="baseUrl+imgUrl" mode="aspectFill"></image>
						<view class="description">
							<view>此处仅供经销商提供个人收款码。</view>
							<view>如因个人操作失误造成损失，请后果自负。</view>
						</view>
					</template>
				</view>
			</view>
			<view class="bottomArea">
				<view class="buttonBox">
					<template v-if="!submitFlag">
						<view class="buttonItem"></view>
					</template>
					<template v-else>
						<view class="buttonItem cancel" @tap="clearQrImage">取消</view>
						<view class="buttonItem comfirm" @tap="submitImage">确认</view>
					</template>
				</view>
			</view>
		</template>
		<loading></loading>
		<tui-modal></tui-modal>
	</view>
</template>

<script>
	import tuiUpload from '@/components/tui-upload/tui-upload.vue';
	export default {
		data() {
			return {
				limit: 1,
				imgUrl: '',
				initData: '',
				updateQrFlag: false,
				submitFlag: false,
				submitTag: '',
				clearFlag: false,
				baseUrl: this.$store.state.imgUrl,
				bucketType: 'es',//上传文件额外参数
			}
		},
		components: {
			tuiUpload
		},
		onLoad() {
			// #ifdef H5
			   this.$wechat.noShare();
			// #endif
			this.getCollectionQrCode();
		},
		methods: {
			getImgResult(e) {
				console.log(e)
				if (e.status == 1 && e.imgArr && e.imgArr.length > 0) {
					this.imgUrl = e.imgArr[0];
					this.submitFlag = true;
					// this.$util.toast('上传成功','success');
				} else if (e.status == 3) {
					this.$util.toast('上传失败，请重新上传');
					this.submitFlag = false;
				}
			},
			submitImage() {
				const params = {
					qrCode: this.imgUrl,
				}
				this.$http.updateCollectionQrCode(params).then((res) => {
					if (res.status == 0) {
						this.submitFlag = false;
						this.getCollectionQrCode();
					}
				})
			},
			clearQrImage() {
				this.imgUrl = '';
				this.clearFlag = true;
				this.submitFlag = false;
			},
			getCollectionQrCode() {
				this.$http.initCollectionQrCode().then((res) => {
					if (res.status == 0) {
						if (res.data != null) {
							this.initData = res.data;
							this.imgUrl = res.data;
							// this.submitFlag = false;
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.myQrCode {
		background: #fff;
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: 60rpx;

		.topArea {
			.noQrImageWrapper {
				color: #999999;
				font-size: 28rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				

				.uploadText {
					color: #111111;
					margin-bottom: 16rpx;
				}

				.description {
					font-size: 24rpx;
					color: #999999;
					margin-top: 30rpx;
					text-align: center;
				}

				.noQrImage {
					width: 400rpx;
					height: 400rpx;
					/* #ifndef MP-WEIXIN */
					margin-left: 28rpx;
					/* #endif */
				}

				.updateImage {
					margin-top: 25rpx;
				}

				.update-text {
					color: #608AFF;
					font-size: 28rpx;
					margin-top: 40rpx;
					text-align: center;
					width: 400rpx;
				}
			}

			.qrImage {
				width: 400rpx;
				height: 400rpx;
			}
		}

		.bottomArea {
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;

			.showText {
				color: #608AFF;
				font-size: 28rpx;
			}

			.buttonBox {
				margin-top: 30rpx;
				margin-bottom: 30rpx;
				display: flex;

				.buttonItem {
					width: 240rpx;
					height: 80rpx;
					border-radius: 50rpx;
					text-align: center;
					line-height: 80rpx;
				}

				.cancel {
					font-size: 32rpx;
					color: #999999;
					background: #F5F5F5;
					margin-right: 40rpx;
				}

				.comfirm {
					font-size: 32rpx;
					color: #fff;
					background: linear-gradient(151deg, #F69E97 0%, #EA655E 100%);
				}
			}
		}

	}
</style>
